//
//  YCLStatusOriginalView.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/14.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit
import SnapKit

class YCLStatusOriginalView: UIView {
    
    //微博模型
    var status:YCLStatus?{
    
        didSet{
        
            //cell将模型传过来由YCLStatusOriginalView自己处理
            
            //设置头像
            if let profile_image_url = status?.user?.profile_image_url {
            
                let url = NSURL.init(string: profile_image_url)
                
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar"))
            }
            
            // 用户名称
            nameLabel.text = status?.user?.screen_name
            
            // 时间
            sendTimeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()
            
            // 来源
            sourcesLabel.text = status?.source
            
            // 微博内容
//            contentLabel.text = status?.text
            contentLabel.attributedText = status?.attrText
            
            //设置用户会员等级
            memberView.image = status?.user?.mbrankImg
//            if let mbrank = status?.user?.mbrank {
//                
//                if mbrank > 0 && mbrank <= 6 {
//                    
//                    memberView.image = UIImage.init(named: "common_icon_membership_level\(mbrank)")
//                }else{
//                
//                    //防止复用
//                    memberView.image = nil
//                }
//            }
            
            //认证类型
              certifyView.image = status?.user?.verifiedImg
//            if let verified_type = status?.user?.verified_type {
            
//                 // 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
//                switch verified_type {
//                case 0:
//                  certifyView.image = UIImage.init(named: "avatar_vip")
//                case 2,3,5:
//                    certifyView.image = UIImage.init(named: "avatar_enterprise_vip")
//                case 220:
//                    certifyView.image = UIImage.init(named: "avatar_grassroot")
//                default:
//                    certifyView.image = nil
//                }
          
//            }
            
            //图片
            statusPictureView.pictureURLs = status?.storePictureURLs
            
            //没有图片的时候，图片与内容label的间距为0
            statusPictureView.snp_updateConstraints { (make) in
                
                let margin = status?.storePictureURLs == nil ? 0 : YCLStatusCellMargin
                
                
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
            
        }
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        //设置ui
        prepareUI()
    }
    
    private func prepareUI() {
    
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(sendTimeLabel)
        addSubview(sourcesLabel)
        addSubview(memberView)
        addSubview(certifyView)
        addSubview(contentLabel)
        addSubview(statusPictureView)

        //设置约束
        iconView.snp_makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(YCLStatusCellMargin)
            make.size.equalTo(CGSize(width: YCLStatusIconWH, height: YCLStatusIconWH))
        }

        nameLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(YCLStatusCellMargin)
        }
        
         //时间
        sendTimeLabel.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源
        sourcesLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(sendTimeLabel.snp_trailing).offset(YCLStatusCellMargin)
            make.centerY.equalTo(sendTimeLabel)
            
        }
        
         //会员等级
        memberView.snp_makeConstraints { (make) in
            
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(YCLStatusCellMargin)
            
            //防止提供的图片修改，造成图片不匹配
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        
         //认证图标
        certifyView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
         //微博内容
        contentLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(self).offset(YCLStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(YCLStatusCellMargin)
            make.trailing.equalTo(self).offset(-YCLStatusCellMargin)
            
        }
        
        // 微博配图视图
        statusPictureView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(self).offset(YCLStatusCellMargin)
//            make.trailing.equalTo(self).offset(-YCLStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(YCLStatusCellMargin)
            
            //给cell固定一些高度
            make.size.equalTo(CGSizeZero)
        }

        //YCLStatusOriginalView的底部由内容多少决定
        self.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(statusPictureView).offset(YCLStatusCellMargin)
        }

        
    }
    
    //MARK 懒加载控件
    
    //头像图片
    private lazy var iconView:UIImageView = {
    
       let imageView =  UIImageView.init(image: UIImage.init(named: "avatar"))
        
        //裁剪图片
        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
        
        return imageView
        
    }()
    
    //用户名
    private lazy var nameLabel:UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(YCLStatusNameFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    //发送微博时间
    private lazy var sendTimeLabel:UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(YCLStatusTimeFontSize)
        label.textColor = UIColor.orangeColor()
        
        return label
        
    }()
    
    //微博来源
    private lazy var sourcesLabel:UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(YCLStatusTimeFontSize)
        label.textColor = UIColor.lightGrayColor()
        
        return label

    }()
    
    //会员图标
    private lazy var memberView:UIImageView = UIImageView.init(image: UIImage.init(named: "common_icon_membership_expired"))
    
    //认证图标
    private lazy var certifyView:UIImageView = UIImageView.init(image: UIImage.init(named: "avatar_vip"))
    
    //原创微博内容
    private lazy var contentLabel:UILabel = {
    
        let label = UILabel()
        
        //字体大小和颜色
        label.font = UIFont.systemFontOfSize(YCLStatusContentFontSize)
        label.textColor = UIColor.grayColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //视图view
    private lazy var statusPictureView:YCLStatusPictureView = YCLStatusPictureView()


}
